<?php


namespace App\common\services;


use App\Helps\LogHelper;

class BaseServices
{
    public $logFile = '';

    /**
     * 生成 信息日志
     *
     * @param string $msg //日志信息
     * @param array $data //打印数据
     * @return
     */
    public function logInfo($msg, $data = null)
    {
        $logPath = 'service_info'.($this->logFile ? '/'.$this->logFile : '');
        LogHelper::info($logPath, $msg, $data);
    }

    /**
     * 生成 错误日志
     *
     * @param string $msg //日志信息
     * @param array $data //打印数据
     * @return
     */
    public function logError($msg, $data = null)
    {
        $logPath = 'service_info'.($this->logFile ? '/'.$this->logFile : '');
        LogHelper::error($logPath, $msg, $data);
    }

    /**
     * 生成唯一单号
     *
     * @param $prefix //前缀一般为两个大写字母
     * @param string $field //字段不为 order_sn 时需要参数field
     * @param int $length //日期后随机数长度
     * @param bool $numeric //受否为纯数字
     * @return string
     */
    public static function createOrderSn($prefix, $field = 'order_sn', $length = 6, $numeric = true)
    {
        $orderSn = createNo($prefix, $length, $numeric);
        while (1) {
            if (!self::where($field, $orderSn)->first()) {
                break;
            }
            $orderSn = createNo($prefix, $length, $numeric);
        }
        return $orderSn;
    }

    /**
     * 返回数据
     * @param $status
     * @param $message
     * @param $data
     * @return array
     */
    public static  function returnSuccess($message = 'success', $data = [])
    {
        return ['status' => 0, 'msg' => $message, 'data' => $data];
    }

    public static  function returnError($status = 101, $message = 'error', $data = [])
    {
        return ['status' => $status, 'msg' => $message, 'data' => $data];
    }

    /**
     * 将数组转换成字符串
     * @param $array
     * @return string|void
     */
    public function arrayToString($array)
    {
        $string = '';
        if (empty($array) || !is_array($array)) {
            return $string;
        }
        return explode(',', $array);
    }

    /**
     * 将字符串转换成数组
     * @param $string
     * @return array|false|string[]
     */
    public function stringToArray($string)
    {
        $array = [];
        if (empty($string)) {
            return $array;
        }
        return explode(',', $string);
    }

}
